package com.example.testapplication.Paging;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;

public class PageListAdapter extends PagedListAdapter<Income, PageListAdapter.IncomeVH> {
    private Context mContext;
    private LayoutInflater layoutInflater;

    private final static DiffUtil.ItemCallback<Income> mDiffCallback = new DiffUtil.ItemCallback<Income>() {
        @Override
        public boolean areItemsTheSame(@NonNull Income oldItem, @NonNull Income newItem) {
            return oldItem.equals(newItem);
        }

        @Override
        public boolean areContentsTheSame(@NonNull Income oldItem, @NonNull Income newItem) {
            return oldItem.getId() == newItem.getId();
        }
    };

    protected PageListAdapter(Context context) {
        super(mDiffCallback);
        mContext = context;
        this.layoutInflater = LayoutInflater.from(mContext);


    }

    @NonNull
    @Override
    public IncomeVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new IncomeVH(layoutInflater.inflate(R.layout.item_income,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull IncomeVH holder, int position) {
        Income income = getItem(position);
        holder.dataTv.setText(income.getData());
        holder.moneyTv.setText("￥" + income.getMoney() + "");

    }

    public class IncomeVH extends RecyclerView.ViewHolder {
        TextView dataTv;
        TextView moneyTv;

        public IncomeVH(@NonNull View itemView) {
            super(itemView);
            dataTv = itemView.findViewById(R.id.dataTv);
            moneyTv = itemView.findViewById(R.id.moneyTv);
        }
    }
}
